package br.biofoco.p2p.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import br.biofoco.p2p.rpc.messaging.WireMessage;

public final class ObjectSerializer implements Serializer {
	
	public ObjectSerializer() {}
	
	public <T extends WireMessage> void serialize(T wireMessage, OutputStream outputStream) throws IOException {
		ObjectOutputStream oos = new ObjectOutputStream(outputStream);
		oos.writeObject(wireMessage);
		oos.flush();			
	}

	//TODO method is returning NULL !!!
	@SuppressWarnings("unchecked")
	@Override
	public <T extends WireMessage> T unserialize(InputStream inputStream, Class<T> value) throws IOException {
		ObjectInputStream ois = new ObjectInputStream(inputStream);					
		T request = null;
		try {
			request = (T) ois.readObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return request;
	}
}
